Imports System.IO

Module DeleteTmpFiles

    Sub ProcessFolder(ByVal Dir As String)
        Dim DirObj As New DirectoryInfo(Dir)

        Dim Files As FileInfo() 
        Files=DirObj.GetFiles("*.tmp")

        Dim Dirs As DirectoryInfo() 
        Dirs= DirObj.GetDirectories("*.*")
 
        Dim Filename As FileInfo
        For Each Filename In Files
            Try
                If (Filename.Attributes And FileAttributes.ReadOnly) Then
                    Filename.Attributes =(Filename.Attributes And Not FileAttributes.ReadOnly)
                End If
             
                File.Delete(Filename.FullName)
               
                Console.WriteLine("Xoa {0}", Filename.FullName)
            Catch E As Exception
                Console.WriteLine("Loi khi xoa file {0}", Filename.FullName)
                Console.WriteLine("Error: {0}", E.Message)
            End Try
        Next

        Dim DirectoryName As DirectoryInfo

        For Each DirectoryName In Dirs
            Try
                ProcessFolder(DirectoryName.FullName)
            Catch E As Exception
                Console.WriteLine("Loi khi xoa thu muc {0}", DirectoryName.FullName)
                Console.WriteLine("Error: {0}", E.Message)
            End Try
        Next

    End Sub

    Sub Main()
        ProcessFolder("C:\Tmp")
    End Sub
End Module
